/*
** map.c for zappy in /home/guidet_m/TECK2/projets/zappy/server_zappy
** 
** Made by michael guidet
** Login   <guidet_m@epitech.net>
** 
** Started on  Tue Jul  5 10:09:26 2011 michael guidet
** Last update Tue Jul  5 10:11:39 2011 michael guidet
*/

#include "server.h"
int	*generate_ressources(t_env *e)
{
  int	*ressource;

  ressource = x_null(NULL, malloc(sizeof(*ressource) * ALL_RC), "malloc");
  ressource[NOURRITURE] = e->height * 0.25f * e->width;
  ressource[LINEMATE] = e->width * e->height / 6;
  ressource[DERAUMERE] = e->width * e->height / 7;
  ressource[SIBUR] = e->width * e->height / 8;
  ressource[MENDIANE] = e->width * e->height / 9;
  ressource[PHIRAS] = e->width * e->height / 10;
  ressource[THYSTAME] = e->width * e->height / 11;
  return (ressource);
}

t_case	**generate_map(int x, int y, int *ressource)
{
  t_case	**map;
  int		i;
  int		j;
  int		k;

  map = x_null(NULL, malloc(sizeof(*map) * y), "malloc");
  for (i = 0; i < y ; i++)
    map[i] = x_null(NULL, malloc(sizeof(*map[i]) * x), "malloc");
  for (i = 0; i < y; i++)
    for (j = 0; j < x; j++)
      map[i][j].players = NULL;
  for (i = 0; i < ALL_RC; i++)
    {
      while (ressource[i] > 0)
	{
	  j = rand() % y;
	  k = rand() % x;
	  map[j][k].ressource[i] += 1;
	  ressource[i]--;
	}
    }
  free(ressource);
  return (map);
}

void	pop_ressources(t_env *e)
{
  t_fd	*tmp;
  char	cmd2[BUF_SIZE];
  float	nb;
  int	x;
  int	y;

  e->time_pop -= timeval_to_int(e->elapsed);
  if (e->time_pop > 0)
    return ;
  e->time_pop = (long)(70.0f / (float)e->t * 1000000.0f);
  for (nb = e->height * 0.05f * e->width; nb > 0; nb--)
    {
      x = rand() % e->width;
      y = rand() % e->height;
      e->map[y][x].ressource[NOURRITURE] += 1;
      sprintf(cmd2, "bct %d %d %d %d %d %d %d %d %d\n", x, y,
	      e->map[y][x].ressource[NOURRITURE], e->map[y][x].ressource[LINEMATE],
	      e->map[y][x].ressource[DERAUMERE], e->map[y][x].ressource[SIBUR],
	      e->map[y][x].ressource[MENDIANE], e->map[y][x].ressource[PHIRAS],
	      e->map[y][x].ressource[THYSTAME]);
      for (tmp = e->fd; tmp; tmp = tmp->next)
	if (tmp->type == GRAPH)
	  add_string_to_list(&tmp->send_list, cmd2);
    }
}

void	move_ressources(t_env *e, int y, int x)
{
  t_fd	*tmp;
  int	i;
  int	ny;
  int	nx;
  char	cmd2[BUF_SIZE];

  for (i = 0; i < ALL_RC; i++)
    {
      while (e->map[y][x].ressource[i])
	{
	  e->map[y][x].ressource[i] -= 1;
	  ny = rand() % e->width;
	  nx = rand() % e->height;
	  e->map[ny][nx].ressource[i] +=1;
	  sprintf(cmd2, "bct %d %d %d %d %d %d %d %d %d\n", nx, ny,
		  e->map[ny][nx].ressource[NOURRITURE],
		  e->map[ny][nx].ressource[LINEMATE],
		  e->map[ny][nx].ressource[DERAUMERE], e->map[ny][nx].ressource[SIBUR],
		  e->map[ny][nx].ressource[MENDIANE], e->map[ny][nx].ressource[PHIRAS],
		  e->map[ny][nx].ressource[THYSTAME]);
	  for (tmp = e->fd; tmp; tmp = tmp->next)
	    if (tmp->type == GRAPH)
	      add_string_to_list(&tmp->send_list, cmd2);
	}
    }
}

void		aff_map(t_case **map, int x, int y)
{
  int	i;
  int	j;

  for (i = 0; i < y; i++)
    for (j = 0; j < x; j++)
      {
	printf("Nourriture : %d\n", map[i][j].ressource[NOURRITURE]);
	printf("Linemate : %d\n", map[i][j].ressource[LINEMATE]);
	printf("derau : %d\n", map[i][j].ressource[DERAUMERE]);
	printf("Sibur : %d\n", map[i][j].ressource[SIBUR]);
	printf("mendiane : %d\n", map[i][j].ressource[MENDIANE]);
	printf("phiras : %d\n", map[i][j].ressource[PHIRAS]);
	printf("THYstame : %d\n", map[i][j].ressource[THYSTAME]);
      }
}
